import java.io.Writer;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-09
 * Time: 22:53
 */
public class demo3 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {

        ReentrantReadWriteLock outer = new ReentrantReadWriteLock();

        //读锁
        ReentrantReadWriteLock.ReadLock readLock = outer.readLock();

        //写锁
        ReentrantReadWriteLock.WriteLock writeLock = outer.writeLock();


        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {

//                writeLock.lock();
                readLock.lock();
                count++;
                readLock.unlock();
//                writeLock.unlock();
            }

        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
//                writeLock.lock();
                readLock.lock();
                count++;
                readLock.unlock();
//                writeLock.unlock();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }
}
